﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;


namespace GGJ.Sprites
{
    class FlySwatter : Ennemy
    {
        private int tileWidth = 80;
        private int tileHeight = 312;
        private Boolean up=true;
        private int speedAttacking=3;
        private float rotation = 0.0f;
        private Vector2 vectorRot;
        private Rectangle hitbox;
        private Boolean hasAttacked = false;

        public FlySwatter(Texture2D texture, int x, int y) : base(texture)
        {
            destination = new Rectangle(
                x,
                y,
                tileWidth,
                tileHeight);

            hitbox.X = (int)350;
            hitbox.Y = (int)0;
            hitbox.Width = 50;
            hitbox.Height = 50;

            vectorRot = new Vector2(destination.Width / 2, destination.Height );
        }

      

        public override void Update(GameTime gameTime, Flies flies,KilledFlies killedFlies,float speed,bool isSlowMotion)
        {          
            destination.X -= (int)speed;

            int realSpeedAttacking = 2*(int)speed;

            if (isSlowMotion)
                realSpeedAttacking = (int)speed / 2;

            if(up)
            {
                destination.Y -= realSpeedAttacking;


                if(destination.Y<300)
                {
                    up = false;
                }
            }

            if(!up)
            {

                
                if (rotation > -Math.PI / 2)
                {
                    if (isSlowMotion)
                        rotation -= 0.25f;
                    else
                        rotation -= 0.5f;
                }
                else  
                {
                    rotation = (float)-Math.PI / 2;
                }



                if (destination.Y < 500)
                {
                    destination.Y += (realSpeedAttacking * 5);
                    hitbox.Y = destination.Y;
                    
                }
                else if (destination.Y >= 500)
                {
                    if (!hasAttacked)
                    {
                        hasAttacked = true;
                        Program.flyswatter.Play();
                    }
                }
                
             
                Fly fly;

                for (int i = 0; i < flies.Count(); i++)
                {
                    fly = flies.GetFly(i);

                    if (fly != null)
                        if (hitbox.Intersects(fly.GetDestination()))
                        {
                            flies.removeFly(i);
                            fly.isDead = true;
                            fly.causeOfDeath = FliesMovement.Swatted;
                            killedFlies.AddFly(fly);
                        }

                }
                
             
            }


            
            
        }

        

        public override void Draw(SpriteBatch spriteBatch)
        {
            Rectangle source;

            source = new Rectangle(56,
                0,
                tileWidth,
                tileHeight);

            spriteBatch.Draw(texture, destination, source, Color.White, rotation,vectorRot , SpriteEffects.None, 0);
             
        }
    }
    
}
